package com.ruoyi.elastic.service.impl;

import com.ruoyi.elastic.service.ESServiceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ESServiceQueryImpl implements ESServiceQuery {
    @Autowired
    private ESService esService;

    private String indexName="skuinfo";
    
    @Override
    public void simpleQuery() {
        String match_all ="{\"query\":{\"match_all\":{}}}";
        String sort = "{\"query\":{\"match_all\":{}},\"sort\":{\"id\":{\"order\":\"desc\"}}}";
        String page = "{\"query\":{\"match_all\":{}},\"sort\":{\"id\":{\"order\":\"desc\"}},\"size\":3,\"from\":0}";
        esService.searchQuery(indexName,match_all);
        esService.searchQuery(indexName,sort);
        esService.searchQuery(indexName,page);
        
    }

    @Override
    public void conditionQuery() {
        //多个域查询
        String match_mu="{\"query\":{\"multi_match\":{\"query\":\"华为\",\"fields\":[\"brandName\",\"name\"]}}}";
        //先进行分词再进行查询
        //通配符 ?代表一个字符，*代表0-n个字符
        String match_wild="{\"query\":{\"wildcard\":{\"brandName\":{\"value\":\"华?\"}}}}";
        String match = "{\"query\":{\"match\":{\"brandName\":\"华为\"}}}";
        String match_prefix = "{\"query\":{\"prefix\":{\"brandName\":\"华为\"}}}";
        String match_term ="{\"query\":{\"term\":{\"brandName\":\"华为\"}}}";
        String match_terms="{\"query\":{\"terms\":{\"brandName\":[\"华为\",\"aaa\"]}}}";
        //涉及时间的注意时间戳问题
        String match_range="{\"query\":{\"range\":{\"createTime\":{\"gte\":1562653438000,\"lte\":1562653438000}}}}";
        String match_exists="{\"query\":{\"exists\":{\"field\":\"name\"}}}";
        esService.searchQuery(indexName,match_wild);
        esService.searchQuery(indexName,match_mu);
        esService.searchQuery(indexName,match_prefix);
        esService.searchQuery(indexName,match_term);
        esService.searchQuery(indexName,match_terms);
        esService.searchQuery(indexName,match_range);
        esService.searchQuery(indexName,match_exists);

    }

    @Override
    public void boolQuery() {
        String match_must="{\"query\":{\"bool\":{\"must\":[{\"term\":{\"brandName\":{\"value\":\"华为\"}}},{\"range\":{\"createTime\":{\"gte\":1562653438000,\"lte\":1562653438000}}}]}}}";
        String match_must_not="{\"query\":{\"bool\":{\"must_not\":[{\"term\":{\"brandName\":{\"value\":\"华为\"}}},{\"range\":{\"createTime\":{\"gte\":1562653438000,\"lte\":1562653438000}}}]}}}";
        String match_should="{\"query\":{\"bool\":{\"should\":[{\"term\":{\"brandName\":{\"value\":\"华为\"}}},{\"range\":{\"createTime\":{\"gte\":1562653438000,\"lte\":1562653438000}}}]}}}";
        esService.searchQuery(indexName,match_must);
        esService.searchQuery(indexName,match_must_not);
        esService.searchQuery(indexName,match_should);

    }

    @Override
    public void groupingQuery() {
        String agg_terms="{\"size\":0,\"query\":{\"bool\":{\"must_not\":[{\"term\":{\"brandName\":{\"value\":\"华为\"}}},{\"range\":{\"createTime\":{\"gte\":1562653438000,\"lte\":1562653438000}}}]}},\"aggs\":{\"name\":{\"terms\":{\"field\":\"categoryId\"}}}}";
        String agg_cardinality="{\"size\":0,\"query\":{\"bool\":{\"must_not\":[{\"term\":{\"brandName\":{\"value\":\"华为\"}}},{\"range\":{\"createTime\":{\"gte\":1562653438000,\"lte\":1562653438000}}}]}},\"aggs\":{\"name\":{\"cardinality\":{\"field\":\"categoryId\"}}}}";
        esService.searchQuery(indexName,agg_terms);
        esService.searchQuery(indexName,agg_cardinality);
    }
}
